package com.kl.controller;


import com.alibaba.fastjson.JSONObject;
import com.kl.domain.FreightDTO;
import com.kl.domain.OrderDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.TabOrder;
import com.kl.feign.OrderServcieFeign;
import com.kl.service.ITabOrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/taborder")
public class TabOrderController {

    @Autowired
    private OrderServcieFeign orderServcieFeign;

    @Autowired
    private ITabOrderService iTabOrderService;


    @ApiOperation(value = "订单表分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页数", required = true,
                    paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,
                    paramType = "query", dataTypeClass = Integer.class)
    })
    @GetMapping("/findAllOrder")
    public Result<List<OrderDTO>> findAllOrder(@RequestParam Integer pageNumber, @RequestParam Integer pageSize){
        Result<List<OrderDTO>> result = orderServcieFeign.findAll(pageNumber, pageSize);
        return result;
    }

    @ApiOperation(value = "根据用户id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true,
                    paramType = "query", dataTypeClass = String.class),
    })
    @GetMapping("/findAllOrderById")
    public Result<List<OrderDTO>> findAllOrderById(@RequestParam String id){
        List<TabOrder> tabOrders = iTabOrderService.findAllOrderById(id);
        String s = JSONObject.toJSONString(tabOrders);
        List<OrderDTO> orderDTOS = JSONObject.parseArray(s, OrderDTO.class);
//        OrderDTO orderDTO = new OrderDTO();
//        BeanUtils.copyProperties(order,orderDTO);;
        return Result.build(ConstantEnum.SUCCESS).setResult(orderDTOS);
    }

    @ApiOperation(value = "根据订单id和手机号查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = false,
                    paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "senderPhone", value = "用户手机号", required = false,
                    paramType = "query", dataTypeClass = String.class),
    })
    @GetMapping("/findOrderByPhoneAndId")
    public Result<List<OrderDTO>> findOrderByPhoneAndId(@RequestParam Long id,@RequestParam String senderPhone){
        List<TabOrder> tabOrders = iTabOrderService.findOrderByPhoneAndId(id,senderPhone);
        String s = JSONObject.toJSONString(tabOrders);
        List<OrderDTO> orderDTOS = JSONObject.parseArray(s, OrderDTO.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(orderDTOS);
    }

}
